package client.domainModels.NodeImpl.test;


import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.domainModels.Node.Node;
import client.domainModels.NodeImpl.Case;
import client.domainModels.NodeImpl.LogFile;
import client.domainModels.NodeImpl.Receivers;
/**
 * 
 * @author Wei WANG
 *
 */
public class MultiLineTableConstructor {

	public static JTable getReceiversTable(Receivers r){
		ArrayList<Node> receviers = r.getChildNames_childObjs().get(r.getChildNodeNames()[0]);
		String[][] a = arrayListToString2(receviers);
		DefaultTableModel model = new DefaultTableModel(a, r.getChildNodeNames());	
		JTable table = new JTable(model);
		return table;
	}
	
	public static String[][] arrayListToString2(ArrayList<Node> al){
		String[][] str = new String[al.size()][1];
		for(int i=0;i<al.size();i++){
			str[i][0]=al.get(i).getID();
		}
		return str;
	}
	


	public static String[][] getRootTableModel(ArrayList<Node> al) {
		String[][] str = new String[al.size()][2];
		for (int i = 0; i < al.size(); i++) {
			str[i][0] = al.get(i).getID();
			str[i][1] = " ";
		}
		return str;
	}

	public static JTable createLogTable(LogFile log) {
		JTable logTable;
		HashMap<Integer, Integer> rowNB_rowHeight = new HashMap<Integer, Integer>();
		if (log.childNames_childObjs.size() > 0) {
			ArrayList<Node> niveau_one_case = log.childNames_childObjs.get("Case");
			String[] titles = log.getRootTableTitles();
			// table non editable
			DefaultTableModel niveau_one_model = new DefaultTableModel(
					getRootTableModel(niveau_one_case), titles){
				public boolean isCellEditable(int row,int col){
		            return false;
		        }

			};
			ArrayList<JTable> eventTables = new ArrayList<JTable>();
			for (int i = 0; i < niveau_one_case.size(); i++) {
				Case caseI = (Case) niveau_one_case.get(i);
				eventTables.add(getEventTables(caseI));
				//System.out.println("height :"+((MultiLineTable) getEventTables(caseI)).getTableHeight());
				rowNB_rowHeight.put(i, ((MultiLineTable) getEventTables(caseI)).getTableHeight());
			}
			logTable = new MultiLineTable(niveau_one_model, eventTables, 1, true);
		} else {
			logTable = new JTable();
		}
		logTable.getColumnModel()
				.getColumn(1)
				.setPreferredWidth(
						logTable.getColumnModel().getColumn(0).getWidth() * 7);

		for(int i=0;i<rowNB_rowHeight.size();i++){
			
			logTable.setRowHeight(i, rowNB_rowHeight.get(i));
		}
		//logTable.setRowHeight(16*5);
		return logTable;
	}
	

	public static MultiLineTable getEventTables(Case c) {
		ArrayList<Node> eventNode = c.getChildNames_childObjs().get("Event");
		String[] titles = { "Event", "Actor", "Task", "Performative",
				"Receivers", "Conversation", "ProjectDate" };
		String[][] datas = new String[eventNode.size()][titles.length];
		ArrayList<JTable> tables = new ArrayList<JTable>();
		HashMap<Integer, Integer> rowNB_rowHeight = new HashMap<Integer, Integer>();
		//HashMap<Node, JTable> event_receiversTable = new HashMap<>();

		for (int j = 0; j < eventNode.size(); j++) {
			datas[j][0]=eventNode.get(j).getID();
			int tmpRowHeight = 16;
			for (int i = 1; i < titles.length; i++) {
				if(eventNode.get(j).getChildNames_childObjs()
						.get(titles[i])!=null&&eventNode.get(j).getChildNames_childObjs()
						.get(titles[i]).size()>0){
					if(titles[i].equals("Receivers")){
						Receivers re = (Receivers) eventNode.get(j).getChildNames_childObjs().get("Receivers").get(0);
						tables.add(getReceiversTable(re));
						if(getReceiversTable(re).getRowCount()>0){
							tmpRowHeight = getReceiversTable(re).getRowCount()*16;
						}
					}
					datas[j][i] = eventNode.get(j).getChildNames_childObjs()
							.get(titles[i]).get(0).getID();
				}else{
					datas[j][i]="";
					if(titles[i].equals("Receivers")){
						tables.add(new JTable());	
					}
					
				}
			}
			rowNB_rowHeight.put(j, tmpRowHeight);
		}
		DefaultTableModel mainTableModel = new DefaultTableModel(datas,titles){
			public boolean isCellEditable(int row,int col){
	            return false;
	        }
		};
		
		MultiLineTable table = new MultiLineTable(mainTableModel, tables, 4, false);
		int tableHeight=0;
		for(int i=0;i<rowNB_rowHeight.size();i++){
			table.setRowHeight(i, rowNB_rowHeight.get(i));
			tableHeight+=rowNB_rowHeight.get(i);
		}
		table.setTableHeight(tableHeight);
		
		return table;
	}
}
